package cn.dlc.com.demo;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 
 * Copyright: Copyright (c) 2020 
 * 
 * @ClassName: StockMain.java
 * @Description: 该类的功能描述
 *
 * @version: v1.0.0
 * @author: 立川
 * @date: 2020年8月24日 上午7:41:29
 */
public class StockMain {
	
	// 定义一把锁
	private static Lock lock = new ReentrantLock();
	
	static class StockThread implements Runnable {

		@Override
		public void run() {
			// 上锁
			lock.lock();
			// 执行业务逻辑
			boolean stock = new Stock().reduceStock();
			// 释放锁
			lock.unlock();
			
			if(stock) {
				System.out.println("减少库存成功");
			} else {
				System.out.println("减少库存失败！");
			}
		}
	}
	
	public static void main(String[] args) {
		StockThread thread = new StockThread();
		thread.run();
		StockThread thread2 = new StockThread();
		thread2.run();
	}
}
